package itsm.isperp.module.controller.api.mobile;

import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.workflow.dto.BpmTransitionLine;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.List;
import java.util.Map;

import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("api/mobile/process")
public class MobileProcessController {

	public static final Logger logger = LoggerFactory
			.getLogger(MobileProcessController.class);

	@Autowired
	BpmProcessRunService bpmProcessRunService;

	@Autowired
	JdbcTemplate jdbcTemplate;

	@RequestMapping("complateTask")
	@ResponseBody
	public ResultMessage complateTask(String username, String id,
			String comment, String nextTranId, int processMode) {
		try {

			ProcessInstance pi = bpmProcessRunService
					.getProcessInstanceByBusinessKey(id);

			if (StringUtils.isBlank(nextTranId)) {
				if (pi != null && processMode == 0) {
					processMode = 1;
				}
			}

			bpmProcessRunService.gotoNextSetp(username, pi
					.getProcessDefinitionId().split(":")[0], null, nextTranId,
					pi.getId(), id, null, null, processMode, 0);

			return ResultMessage.success();

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("处理失败，原因是：" + e.getMessage());
		}

	}

	@RequestMapping("getNextSetps")
	@ResponseBody
	public ResultMessage getNextSetps(String username, String id) {
		try {

			List<BpmTransitionLine> lines = bpmProcessRunService.getNextSteps(
					username, id);
			return ResultMessage.success(JsonBuilder.buildNormalBinder()
					.toJson(lines));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("无法获取下个步骤，原因是：" + e.getMessage());
		}

	}

	/**
	 * 获取待办事项列表
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public ResultMessage list(String username) {

		try {
			// to do
			JqGridRequest request = new JqGridRequest();
			request.setPageable(false);
			request.setSelectColumns("BUSINESS_KEY_", "TITLE", "KEY_",
					"CREATE_DATE", "STATUS");

			request.setSort("CREATE_DATE", Direction.DESC);

			List<Map<String, Object>> list = bpmProcessRunService
					.getAllTodoList(username, request);

			return ResultMessage.success(JsonBuilder.buildNormalBinder()
					.toJson(list));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("获取待办事项列表失败，原因是：" + e.getMessage());
		}

	}

	/**
	 * 获取待办事项列表
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping("listDone")
	@ResponseBody
	public ResultMessage listDone(String username) {

		try {
			// to do
			JqGridRequest request = new JqGridRequest();
			request.setPageable(false);
			request.setSelectColumns("BUSINESS_KEY_", "TITLE", "KEY_",
					"CREATE_DATE", "STATUS");

			request.setSort("CREATE_DATE", Direction.DESC);

			List<Map<String, Object>> list = bpmProcessRunService
					.getAllTodoneList(username, request);

			return ResultMessage.success(JsonBuilder.buildNormalBinder()
					.toJson(list));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("获取已办事项列表失败，原因是：" + e.getMessage());
		}

	}

	/**
	 * 获取已办事项列表
	 * 
	 * @param username
	 *            用户名
	 * @param currentPage
	 * 
	 * @param pageSize
	 *            加载数量（默认20）
	 * @return
	 */
	@RequestMapping("listHandled")
	@ResponseBody
	public ResultMessage listHandled(String username, Integer currentPage,
			Integer pageSize) {

		try {
			// to do
			JqGridRequest request = new JqGridRequest();
			request.setPageable(true);
			request.setPage(currentPage);
			request.setRows(pageSize);
			request.setSort("create_date", Direction.DESC);

			List<Map<String, Object>> list = bpmProcessRunService
					.getAllTodoneList(username, request);

			return ResultMessage.success(JsonBuilder.buildNormalBinder()
					.toJson(list));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("获取已办事项列表失败，原因是：" + e.getMessage());
		}

	}

	/**
	 * 获取待办事项列表
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping("listCount")
	@ResponseBody
	public ResultMessage listCount(String username) {

		try {
			// to do
			JqGridRequest request = new JqGridRequest();
			request.setPageable(false);

			Integer listCount = bpmProcessRunService.getAllTodoCount(username,
					request);

			return ResultMessage.success(JsonBuilder.buildNormalBinder()
					.toJson(listCount));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("获取待办事项总数失败，原因是：" + e.getMessage());
		}

	}

	/**
	 * 获取已办事项列表
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping("listDoneCount")
	@ResponseBody
	public ResultMessage listDoneCount(String username) {

		try {
			// to do
			JqGridRequest request = new JqGridRequest();
			request.setPageable(false);

			Integer listDoneCount = bpmProcessRunService.getAllTodoneCount(
					username, request);

			return ResultMessage.success(JsonBuilder.buildNormalBinder()
					.toJson(listDoneCount));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResultMessage.failure("获取已办事项总数失败，原因是：" + e.getMessage());
		}

	}

}
